import java.util.HashMap;
import java.util.Iterator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;


public class SettingsTable {

	private TabFolder tabTypeViewSettings;
	private Property dataSuppliers;
	private String[] titles = {"supplier"};

	public SettingsTable(Shell shell, TabFolder tabTypeViewSettings, Property dataSuppliers) {
		this.tabTypeViewSettings = tabTypeViewSettings;
		this.dataSuppliers = dataSuppliers;
	}

	public void createContents(){
		createTabItemSection();
		Composite parentTable = (Composite)tabTypeViewSettings.getItem(1).getControl();
		Table table = createTable(parentTable);
		initializationTable(table);
	}

	private void initializationTable(Table table) {
		Iterator<String> itr = dataSuppliers.getKeysSort().iterator();
		while(itr.hasNext()){
			String key = itr.next();
			if (table.getItemCount() > 1 && key.substring(0, key.indexOf(".")).equals(table.getItem(table.getItemCount() - 1).getText(0))){
				continue;
			}
			TableItem item = new TableItem(table, SWT.NONE);
			String[] textItem = new String[titles.length];
			textItem[0] = key.substring(0, key.indexOf("."));
			item.setText(textItem);
		}
		for (TableColumn column : table.getColumns()){
			column.pack();	
		}
		
		final HashMap<Label, Text> map = new HashMap<Label, Text>();
		final Composite panel = new Composite((Composite)tabTypeViewSettings.getItem(1).getControl(), SWT.NONE);
		panel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
		panel.setLayout(new GridLayout(4, false));

		table.addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				for (Label label : map.keySet()){
					map.get(label).dispose();
					label.dispose();
				}
				panel.pack();
				TableItem item = (TableItem)e.item;
				String key = item.getText(0);
				Iterator<String> itr = dataSuppliers.getKeysSort().iterator();
				while (itr.hasNext()){
					String nameSupplier = (String)itr.next();
					if (key.equals(nameSupplier.substring(0, nameSupplier.indexOf(".")))){
						String setting = nameSupplier.substring(nameSupplier.indexOf(".") + 1);
						String value = dataSuppliers.getDataSettings().getProperty(nameSupplier);
						Label label = new Label(panel, SWT.NONE);
						label.setText(setting);
						Text t = new Text(panel, SWT.BORDER);
						t.setText(value);
						GridData gd = new GridData(SWT.FILL, SWT.FILL, false, false);
						gd.heightHint = 20;
						gd.widthHint = 150;
						t.setLayoutData(gd);
						map.put(label, t);
					}
				}
				panel.pack();
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
	}

	private void createTabItemSection() {
		TabItem item = new TabItem (tabTypeViewSettings, SWT.NONE);
		item.setText ("Table");
		Composite composite = new Composite(tabTypeViewSettings, SWT.BORDER | SWT.MULTI);
		composite.setLayout(new GridLayout());
		composite.setBackground(getParentColor(composite));
		item.setControl(composite);
	}

	private Color getParentColor(Composite parent) {
		return parent.getParent().getBackground();
	}

	private Table createTable(Composite parent) {
		Table table = new Table (parent, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible (true);
		table.setHeaderVisible (true);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
		data.heightHint = 200;
		table.setLayoutData(data);
		for (int i = 0; i < titles.length; i++) {
			TableColumn column = new TableColumn (table, SWT.NONE);
			column.setText (titles[i]);
		}
		return table;
	}
}
